package cn.iocoder.yudao.module.infrastructure.user.convert;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infrastructure.dept.controller.admin.vo.dept.DeptSimpleRespVO;
import cn.iocoder.yudao.module.infrastructure.dept.controller.admin.vo.post.PostSimpleRespVO;
import cn.iocoder.yudao.module.infrastructure.dept.dal.dataobject.DeptDO;
import cn.iocoder.yudao.module.infrastructure.dept.dal.dataobject.PostDO;
import cn.iocoder.yudao.module.infrastructure.permission.dal.dataobject.RoleDO;
import cn.iocoder.yudao.module.infrastructure.permission.vo.RoleSimpleRespVO;
import cn.iocoder.yudao.module.infrastructure.user.dal.dataobject.AdminUserDO;
import cn.iocoder.yudao.module.infrastructure.user.vo.AdminUserProfileRespVO;
import cn.iocoder.yudao.module.infrastructure.user.vo.AdminUserRespVO;
import cn.iocoder.yudao.module.infrastructure.user.vo.AdminUserSimpleRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface AdminUserConvert {

    AdminUserConvert INSTANCE = Mappers.getMapper(AdminUserConvert.class);

    default List<AdminUserRespVO> convertList(List<AdminUserDO> list, Map<Long, DeptDO> deptMap) {
        return CollectionUtils.convertList(list, user -> convert(user, deptMap.get(user.getDeptId())));
    }

    default AdminUserRespVO convert(AdminUserDO user, DeptDO dept) {
        AdminUserRespVO userVO = BeanUtils.toBean(user, AdminUserRespVO.class);
        if (dept != null) {
            userVO.setDeptName(dept.getName());
        }
        return userVO;
    }

    default List<AdminUserSimpleRespVO> convertSimpleList(List<AdminUserDO> list, Map<Long, DeptDO> deptMap) {
        return CollectionUtils.convertList(list, user -> {
            AdminUserSimpleRespVO userVO = BeanUtils.toBean(user, AdminUserSimpleRespVO.class);
            MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> userVO.setDeptName(dept.getName()));
            return userVO;
        });
    }

    default AdminUserProfileRespVO convert(AdminUserDO user, List<RoleDO> userRoles, DeptDO dept, List<PostDO> posts) {
        AdminUserProfileRespVO userVO = BeanUtils.toBean(user, AdminUserProfileRespVO.class);
        userVO.setRoles(BeanUtils.toBean(userRoles, RoleSimpleRespVO.class));
        userVO.setDept(BeanUtils.toBean(dept, DeptSimpleRespVO.class));
        userVO.setPosts(BeanUtils.toBean(posts, PostSimpleRespVO.class));
        return userVO;
    }

}
